home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / defaults.cc < prev    next >
C/C++ Source or Header  |  1997-08-13  |  9KB  |  460 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. /*
  24.  
  25. The function builtin_pwd adapted from a similar function from GNU
  26. Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free
  27. Software Foundation, Inc.
  28.  
  29. */
  30.  
  31. /* Modified by Klaus Gebhardt, 1996 - 1997 */
  32.  
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36.  
  37. #include <iostream.h>
  38.  
  39. #include <cstdlib>
  40.  
  41. #include <string>
  42.  
  43. #ifdef HAVE_UNISTD_H
  44. #ifdef HAVE_SYS_TYPES_H
  45. #include <sys/types.h>
  46. #endif
  47. #include <unistd.h>
  48. #endif
  49.  
  50. #include <defaults.h>
  51. #include "defun.h"
  52. #include "error.h"
  53. #include "gripes.h"
  54. #include "help.h"
  55. #include "ov.h"
  56. #include "toplev.h"
  57. #include "variables.h"
  58. #include <version.h>
  59.  
  60. string Voctave_home;
  61.  
  62. string Vbin_dir;
  63. string Vlib_dir;
  64. string Vinfo_dir;
  65. string Varch_lib_dir;
  66. string Vlocal_arch_lib_dir;
  67. string Vfcn_file_dir;
  68.  
  69. // The path that will be searched for programs that we execute.
  70. // (--exec-path path)
  71. string Vexec_path;
  72.  
  73. // Load path specified on command line.
  74. // (--path path; -p path)
  75. string Vload_path;
  76.  
  77. // Name of the editor to be invoked by the edit_history command.
  78. string Veditor;
  79.  
  80. string Vimagepath;
  81.  
  82. string Vlocal_site_defaults_file;
  83. string Vsite_defaults_file;
  84.  
  85. static string
  86. subst_octave_home (const string& s)
  87. {
  88.   string retval;
  89.  
  90.   string prefix = OCTAVE_PREFIX;
  91.  
  92.   retval = s;
  93.  
  94.   if (Voctave_home != prefix)
  95.     {
  96.       int len = prefix.length ();
  97.       size_t start = 0;
  98.       while ((start = retval.find (prefix, start)) != NPOS)
  99.     {
  100.       retval.replace (start, len, Voctave_home);
  101.       start += len;
  102.     }
  103.     }
  104.  
  105.   return retval;
  106. }
  107.  
  108. static void
  109. set_octave_home (void)
  110. {
  111.   char *oh = getenv ("OCTAVE_HOME");
  112.  
  113.   Voctave_home = oh ? string (oh) : string (OCTAVE_PREFIX);
  114. }
  115.  
  116. static void
  117. set_default_info_dir (void)
  118. {
  119.   Vinfo_dir = subst_octave_home (OCTAVE_INFODIR);
  120. }
  121.  
  122. static void
  123. set_default_arch_lib_dir (void)
  124. {
  125.   Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR);
  126. }
  127.  
  128. static void
  129. set_default_local_arch_lib_dir (void)
  130. {
  131.   Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR);
  132. }
  133.  
  134. static void
  135. set_default_fcn_file_dir (void)
  136. {
  137.   Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR);
  138. }
  139.  
  140. static void
  141. set_default_bin_dir (void)
  142. {
  143.   Vbin_dir = subst_octave_home (OCTAVE_BINDIR);
  144. }
  145.  
  146. static void
  147. set_default_lib_dir (void)
  148. {
  149.   Vlib_dir = subst_octave_home (OCTAVE_LIBDIR);
  150. }
  151.  
  152. static void
  153. set_default_exec_path (void)
  154. {
  155.   char *octave_exec_path = getenv ("OCTAVE_EXEC_PATH");
  156.  
  157.   if (octave_exec_path)
  158.     Vexec_path = string (octave_exec_path);
  159.   else
  160.     {
  161.       char *shell_path = getenv ("PATH");
  162.  
  163.       if (shell_path)
  164.     {
  165.       Vexec_path = string (SEPCHAR_STR);
  166.       Vexec_path.append (shell_path);
  167.     }
  168.     }
  169. }
  170.  
  171. // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS.
  172. // If the path starts with `:', prepend the standard path.  If it ends
  173. // with `:' append the standard path.  If it begins and ends with
  174. // `:', do both (which is useless, but the luser asked for it...).
  175.  
  176. static void
  177. set_default_path (void)
  178. {
  179.   string std_path = subst_octave_home (OCTAVE_FCNFILEPATH);
  180.  
  181.   char *oct_path = getenv ("OCTAVE_PATH");
  182.  
  183.   Vload_path = oct_path ? string (oct_path) : std_path;
  184. }
  185.  
  186. static void
  187. set_default_info_file (void)
  188. {
  189.   string std_info_file = subst_octave_home (OCTAVE_INFOFILE);
  190.  
  191.   char *oct_info_file = getenv ("OCTAVE_INFO_FILE");
  192.  
  193.   Vinfo_file = oct_info_file ? string (oct_info_file) : std_info_file;
  194. }
  195.  
  196. static void
  197. set_default_info_prog (void)
  198. {
  199.   char *oct_info_prog = getenv ("OCTAVE_INFO_PROGRAM");
  200.  
  201.   if (oct_info_prog)
  202.     Vinfo_prog = string (oct_info_prog);
  203.   else
  204.     {
  205. #if !defined (__EMX__)
  206.       Vinfo_prog = Varch_lib_dir;
  207.       Vinfo_prog.append ("/info");
  208. #else
  209.       Vinfo_prog = "";
  210. #endif
  211.     }
  212. }
  213.  
  214. static void
  215. set_default_editor (void)
  216. {
  217. #if defined (__EMX__) && defined (OS2)
  218.   Veditor = "e";
  219. #else
  220.   Veditor = "emacs";
  221. #endif
  222.  
  223.   char *env_editor = getenv ("EDITOR");
  224.  
  225.   if (env_editor && *env_editor)
  226.     Veditor = string (env_editor);
  227. }
  228.  
  229. static void
  230. set_local_site_defaults_file (void)
  231. {
  232.   Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR);
  233.   Vlocal_site_defaults_file.append ("/octaverc");
  234. }
  235.  
  236. static void
  237. set_site_defaults_file (void)
  238. {
  239.   Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR);
  240.   Vsite_defaults_file.append ("/octaverc");
  241. }
  242.  
  243. string
  244. maybe_add_default_load_path (const string& pathstring)
  245. {
  246.   string std_path = subst_octave_home (OCTAVE_FCNFILEPATH);
  247.  
  248.   string retval;
  249.  
  250.   if (! pathstring.empty ())
  251.     {
  252.       if (pathstring[0] == SEPCHAR)
  253.     {
  254.       retval = std_path;
  255.       retval.append (pathstring);
  256.     }
  257.       else
  258.     retval = pathstring;
  259.  
  260.       if (pathstring[pathstring.length () - 1] == SEPCHAR)
  261.     retval.append (std_path);
  262.     }
  263.  
  264.   return retval;
  265. }
  266.  
  267. void
  268. install_defaults (void)
  269. {
  270.   // OCTAVE_HOME must be set first!
  271.  
  272.   set_octave_home ();
  273.  
  274.   set_default_info_dir ();
  275.  
  276.   set_default_arch_lib_dir ();
  277.  
  278.   set_default_local_arch_lib_dir ();
  279.  
  280.   set_default_fcn_file_dir ();
  281.  
  282.   set_default_bin_dir ();
  283.  
  284.   set_default_lib_dir ();
  285.  
  286.   set_default_exec_path ();
  287.  
  288.   set_default_path ();
  289.  
  290.   set_default_info_file ();
  291.  
  292.   set_default_info_prog ();
  293.  
  294.   set_default_editor ();
  295.  
  296.   set_local_site_defaults_file ();
  297.  
  298.   set_site_defaults_file ();
  299. }
  300.  
  301. static int
  302. editor (void)
  303. {
  304.   int status = 0;
  305.  
  306.   string s = builtin_string_variable ("EDITOR");
  307.  
  308.   if (s.empty ())
  309.     {
  310.       gripe_invalid_value_specified ("EDITOR");
  311.       status = -1;
  312.     }
  313.   else
  314.     Veditor = s;
  315.  
  316.   return status;
  317. }
  318.  
  319. static int
  320. exec_path (void)
  321. {
  322.   int status = 0;
  323.  
  324.   string s = builtin_string_variable ("EXEC_PATH");
  325.  
  326.   if (s.empty ())
  327.     {
  328.       gripe_invalid_value_specified ("EXEC_PATH");
  329.       status = -1;
  330.     }
  331.   else
  332.     {
  333.       Vexec_path = s;
  334.  
  335.       string std_path = Vlocal_arch_lib_dir;
  336.       std_path.append (SEPCHAR_STR);
  337.       std_path.append (Varch_lib_dir);
  338.  
  339.       int std_len = std_path.length ();
  340.  
  341.       static char *putenv_cmd = 0;
  342.  
  343.       delete [] putenv_cmd;
  344.  
  345.       putenv_cmd = 0;
  346.  
  347.       int eplen = Vexec_path.length ();
  348.  
  349.       if (eplen > 0)
  350.     {
  351.       int prepend = (Vexec_path[0] == SEPCHAR);
  352.       int append = (eplen > 1 && Vexec_path[eplen-1] == SEPCHAR);
  353.  
  354.       if (prepend)
  355.         {
  356.           if (append)
  357.         {
  358.           putenv_cmd = new char [2 * std_len + eplen + 6];
  359.           sprintf (putenv_cmd, "PATH=%s%s%s",
  360.                std_path.c_str (), Vexec_path.c_str (),
  361.                std_path.c_str ());
  362.         }
  363.           else
  364.         {
  365.           putenv_cmd = new char [std_len + eplen + 6];
  366.           sprintf (putenv_cmd, "PATH=%s%s",
  367.                std_path.c_str (), Vexec_path.c_str ());
  368.         }
  369.         }
  370.       else
  371.         {
  372.           if (append)
  373.         {
  374.           putenv_cmd = new char [std_len + eplen + 6];
  375.           sprintf (putenv_cmd, "PATH=%s%s",
  376.                Vexec_path.c_str (), std_path.c_str ());
  377.         }
  378.           else
  379.         {
  380.           putenv_cmd = new char [eplen + 6];
  381.           sprintf (putenv_cmd, "PATH=%s", Vexec_path.c_str ());
  382.         }
  383.         }
  384.     }
  385.       else
  386.     {
  387.       putenv_cmd = new char [std_len+6];
  388.       sprintf (putenv_cmd, "PATH=%s", std_path.c_str ());
  389.     }
  390.  
  391.       putenv (putenv_cmd);
  392.     }
  393.  
  394.   return status;
  395. }
  396.  
  397. static int
  398. imagepath (void)
  399. {
  400.   int status = 0;
  401.  
  402.   string s = builtin_string_variable ("IMAGEPATH");
  403.  
  404.   if (s.empty ())
  405.     {
  406.       gripe_invalid_value_specified ("IMAGEPATH");
  407.       status = -1;
  408.     }
  409.   else
  410.     Vimagepath = s;
  411.  
  412.   return status;
  413. }
  414.  
  415. static int
  416. octave_loadpath (void)
  417. {
  418.   int status = 0;
  419.  
  420.   string s = builtin_string_variable ("LOADPATH");
  421.  
  422.   if (s.empty ())
  423.     {
  424.       gripe_invalid_value_specified ("LOADPATH");
  425.       status = -1;
  426.     }
  427.   else
  428.     Vload_path = maybe_add_default_load_path (s);
  429.  
  430.   return status;
  431. }
  432.  
  433. void
  434. symbols_of_defaults (void)
  435. {
  436.   DEFVAR (EDITOR, Veditor, 0, editor,
  437.     "name of the editor to be invoked by the edit_history command");
  438.  
  439.   DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path,
  440.     "colon separated list of directories to search for programs to run");
  441.  
  442.   DEFVAR (LOADPATH, Vload_path, 0, octave_loadpath,
  443.     "colon separated list of directories to search for scripts");
  444.  
  445.   DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath,
  446.     "colon separated list of directories to search for image files");
  447.  
  448.   DEFCONST (OCTAVE_HOME, Voctave_home, 0, 0,
  449.     "top-level Octave installation directory");
  450.  
  451.   DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0,
  452.     "Octave version");
  453. }
  454.  
  455. /*
  456. ;;; Local Variables: ***
  457. ;;; mode: C++ ***
  458. ;;; End: ***
  459. */
  460.